//  g++ -o cImgTest.exe cImgTest.cpp -O2 -L/usr/X11R6/lib -lm -lpthread -lX11
#include "CImg.h" 
using namespace cimg_library; 

////////////////////////////////////////////////////////////////////////////////
void test01()
{
    int size_x = 640;
    int size_y = 480;
    int size_z = 1;
    int numberOfColorChannels = 3; // B&W
    unsigned char initialValue = 0;

    CImg<unsigned char> image(size_x, size_y, size_z, numberOfColorChannels, initialValue);

    CImgDisplay display(image, "Click a point");

    while (!display.is_closed())
    {
        display.wait();
        if (display.button() && display.mouse_y() >= 0 && display.mouse_x() >= 0)
        {
            const int y = display.mouse_y();
            const int x = display.mouse_x();

            unsigned char randomColor[3];
            randomColor[0] = rand() % 256;
            randomColor[1] = rand() % 256;
            randomColor[2] = rand() % 256;

            image.draw_point(x, y, randomColor);
        }
        image.display(display);
    }
}   
////////////////////////////////////////////////////////////////////////////////    
// black and white image
void test02()
{
    int size_x = 640;
    int size_y = 480;
    int size_z = 1;
    int numberOfColorChannels = 1; // B&W
    unsigned char initialValue = 0;

    CImg<unsigned char> image(size_x, size_y, size_z, numberOfColorChannels, initialValue);

    CImgDisplay display(image, "Click a point");

    while (!display.is_closed())
    {
        display.wait();
        if (display.button() && display.mouse_y() >= 0 && display.mouse_x() >= 0)
        {
            const int y = display.mouse_y();
            const int x = display.mouse_x();
           
            image(x,y,0)=255;
        }
        image.display(display);
    }
}

////////////////////////////////////////////////////////////////////////////////    
// save and display black and white image
void test03()
{
    // define black and white image
    unsigned int size_x = 640;
    unsigned int size_y = 480;
    unsigned int size_z = 1;
    int numberOfColorChannels = 1; // B&W
    unsigned char initialValue = 0; // black
    
    CImg<unsigned char> image(size_x, size_y, size_z, numberOfColorChannels, initialValue);

    // draw to image
    unsigned char greyShade=0;
    for( unsigned int itX=0; itX<size_x; ++itX)
    {
        for( unsigned int itY=0; itY<size_y; ++itY)
        {
            image(itX,itY,0)=greyShade;
        }
        
        ++greyShade;
    }

    // save image
    image.save("cImgTest.png");
    
    // display image
    CImgDisplay display(image, "test03");
    while (!display.is_closed())
    {
        display.wait();       
        image.display(display);
    }
}

////////////////////////////////////////////////////////////////////////////////
int main(void)
{
    //~ test01();
    //~ test02();
    test03();
 
    return 0;
}
